from appium import webdriver

class CommonDriver:
    def get_driver(self):
        #设置连接信息
        desc={
            "platformName":"Android",                               #操作系统的名字
            "platformVersion":"7.1.2",                              #操作系统版本的号
            "appPackage":"com.dangdang.buy2",                       #包名
            "appActivity":"com.dangdang.buy2.StartupActivity",      #Activity名
            "udid":"127.0.0.1:21523",                               #设备名
        }
        #Appium Server的服务地址
        command_executor_url = 'http://127.0.0.1:4723/wd/hub'
        #打开app
        driver = webdriver.Remote(command_executor=command_executor_url,
                                  desired_capabilities=desc)
        driver.implicitly_wait(20)
        #返回driver
        return driver

if __name__ == "__main__":
    import  time
    from appium.webdriver.common.appiumby import AppiumBy
    from appium.webdriver.common.touch_action import TouchAction
    from click import command
    from selenium.webdriver.support.wait import WebDriverWait
    from telnetlib import EC

    # 打开app
    driver = CommonDriver().get_driver()
    el1 = driver.find_element(by=AppiumBy.ID, value="com.dangdang.buy2:id/tv_sure")
    el1.click()
    print(f'当前时间为{time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())}')
    try:
        # WebDriverWait(driver,2000) 设置显示等待，最大等待时间为2000秒
        # until(条件)  直到满足xx条件
        # EC.visibility_of_element_located(元组)   定位元组中描述的元素
        # (AppiumBy.ID,'值') 定义定位方式和对应的值
        el2 = WebDriverWait(driver, 2000).until(
            EC.visibility_of_element_located((AppiumBy.ID, 'com.dangdang.buy2:id/tv_sure')))
        el2.click()
    except Exception as e:
        print(e)
    finally:
        print(f'当前时间为{time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())}')
    # # 定位一元素
    # el4 = driver.find_element(by=AppiumBy.ID, value='com.dangdang.buy2:id/et_all')
    # # 实例化一个对角用于按住和拖动
    # a = TouchAction(driver)
    # # long-press(元素)，按住元素
    # # move_to(X,Y)，移动到指定的坐标
    # # release(),松开按住的元素
    # # perform(),立即执行
    # a.long_press(el4).move_to(x=510, y=568 - 300).release().perform()
    # time.sleep(3)
    # a.long_press(el4).move_to(x=510, y=568 + 300).release().perform()
    # time.sleep(3)
    # driver.swipe(300, 500, 300, 500 - 300)
    # time.sleep(3)
    # driver.swipe(300, 500, 300, 500 + 300)
